package cst.a810lw.zyfypt_810lw.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import cst.a810lw.zyfypt_810lw.Listener.AmicoolListener;
import cst.a810lw.zyfypt_810lw.Model.ResModel;
import cst.a810lw.zyfypt_810lw.R;

public class RegActivity extends AppCompatActivity {
    private EditText username;
    private EditText password;
    private EditText tel;
    private EditText realname;
    private EditText email;

    private Button btn_confirm;
    private Button btn_return;

    private AmicoolListener<String> listener;
    private ResModel model;
    private Intent intent;
    private RadioButton reg_student;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);

        username = (EditText) findViewById(R.id.login_username);
        password = (EditText) findViewById(R.id.login_password);
        tel = (EditText) findViewById(R.id.login_Tel);
        realname = (EditText) findViewById(R.id.login_realname);
        email = (EditText) findViewById(R.id.login_email);

        btn_confirm = (Button) findViewById(R.id.btn_confirm);
        btn_return = (Button) findViewById(R.id.btn_return);
        reg_student = (RadioButton) findViewById(R.id.reg_student);

        intent=new Intent(RegActivity.this,LoginActivity.class);

        listener=new AmicoolListener<String>() {
            @Override
            public void onResponse(String list) {
                if(list.equals("1")){
                    Toast.makeText(RegActivity.this,"注册成功，返回登陆",Toast.LENGTH_SHORT).show();
                    startActivity(intent);
                    finish();
                }
                else
                    Toast.makeText(RegActivity.this,"用户已经存在",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFail(String Massage) {
                Toast.makeText(RegActivity.this,Massage,Toast.LENGTH_SHORT).show();
            }
        };
        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                model=new ResModel("scalar");
                if(reg_student.isChecked())
                    model.reg(username.getText().toString(),password.getText().toString(),tel.getText().toString(),2,realname.getText().toString(),
                            email.getText().toString(),listener);
                else
                    model.reg(username.getText().toString(),password.getText().toString(),tel.getText().toString(),3,realname.getText().toString(),
                            email.getText().toString(),listener);
            }
        });


        btn_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(intent);
                finish();
            }
        });
    }
}
